
import {
    get,
    post
  } from '@/utils/request'
  export default {
    // 命名空间
    namespaced: true,
    // 保存我们要使用的数据
    state: {
      infos: [],
      infoCatogory: [],
      //发送网络请求
    },
    getters: {
      my_infos(state) {
        let i = 0
        // console.log(state.infos)
        // console.log(state.infoCatogory)
        for (i = 0; i < state.infos.length; i++) {
          let flag = 0, j = 0;
          for (j = 0; j < state.infoCatogory.length; j++) {
            if (state.infos[i].categoryId == state.infoCatogory[j].id) {
              flag = 1;
              break;
            }
          }
          if (flag == 1) {
            state.infos[i].name = state.infoCatogory[j].name
          }
        }
        return state.infos
      }
    },
    // 将数据进行赋值
    mutations: {
      //产品数据对象
      SET_INFO(state, data) {
        state.infos = data;
      },
      SET_TNIFOCATEGORY(state, data) {
        state.infoCatogory = data;
      },
      FIND_ARTICLE_BY_TITLE(state, value) {
        state.infos = state.infos.filter((info) => {
          return info.title.indexOf(value) != -1
        })
      }
    },
    actions: {
      // 获取所有咨询的数据
      async getInfoVuex({ commit }) {
        // 1. 发送网络请求
        // 2. 获取对应的数据
        // 3. 提交到mutations中
        let res = await get('/article/findAll')
        //  console.log(res)
        commit('SET_INFO', res.data)
      },//分类咨询
      async getInfoCatogoryVuex({ commit }) {
        let res = await get('/category/findAll')
        // console.log(res)
        commit('SET_TNIFOCATEGORY', res.data)
      },
    }
  }
  